const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    disabled: true,
    isLock: null,
    userInfo: null,
    idactive:0,
    responsibleList: [],
    queryFrom: {
      hospitalId: '',
      hospitalName: '',
      cooperationType: '',
      salesPerson:''
    }
  },
  bindSearchChange(e) {
    let that = this
    console.log(e);
    that.setData({
      "queryFrom.cooperationType": e.detail.doctorsId,
      "queryFrom.salesPerson": e.detail.treatment,
      "queryFrom.province": e.detail.territoryId,
    })
   that.getTyoeList()
  },
  getTyoeList(valid,istype) {
    let that = this
    if(wx.getStorageSync('tabNav')==3){
      wx.request({
        url:(wx.getStorageSync('tabNav')==3?app.globalData.serverUrlgdb:app.globalData.serverUrl) + '/applet/hos/list',
        method: 'post',
        header: {
          'token': wx.getStorageSync('token')
        },
        data:that.data.queryFrom,
        success: res => {
          let resp = res.data
          if (resp.code == 200) {
            resp.data.hospitalList.map(item => {
              item.checked = false
              if(valid){
                if (item.hospitalId == valid.hospitalId) {
                  console.log(item,'数据详情');
                  that.getkslist(item.hospitalId,valid.deptId)
                  item.checked = true
                  that.setData({
                    disabled: false,
                    idactive:item.hospitalId
                  })
                }else{
                  item.checked = false
                }
              }else{
                if (item.hospitalId == that.data.userInfo.hospitalId) {
                  item.checked = true
                  that.setData({
                    disabled: false,
                    idactive:item.hospitalId
                  })
                }else{
                  item.checked = false
                }
              }
            })
            that.setData({
              responsibleList:resp.data.hospitalList,
            })
            if(that.data.responsibleList.length==0){
              that.setData({
                disabled:true
              })
            }else{
              that.setData({
                disabled:false
              })
            }
          }
        }
      })
    }else{
      wx.request({
        url: (wx.getStorageSync('tabNav')==3?app.globalData.serverUrlgdb:app.globalData.serverUrl) + '/st/sort/'+(istype?istype:'1'),
        method: 'get',
        header: {
        'token': wx.getStorageSync('token')
      },
      success: res => {
        let resp = res.data
        if (resp.code == 200) {
          resp.data.hospitalList.map(item => {
            item.checked = false
            if(valid){
              if (item.hospitalId == valid.hospitalId) {
                that.getkslist(item.hospitalId,valid.deptId)
                item.checked = true
                that.setData({
                  disabled: false,
                  idactive:item.hospitalId
                })
              }else{
                item.checked = false
              }
            }else{
              if (item.hospitalId == that.data.userInfo.hospitalId) {
                item.checked = true
                that.setData({
                  disabled: false,
                  idactive:item.hospitalId
                })
              }else{
                item.checked = false
              }
            }
          })
          let responsibleLists = []
          if(that.data.queryFrom.cooperationType!=''){
            resp.data.hospitalList.map(item=>{
              if(item.cooperationType==that.data.queryFrom.cooperationType){
                responsibleLists.push(item)
              }
            })
          }else{
            responsibleLists = resp.data.hospitalList
          }
          that.setData({
            responsibleList:responsibleLists,
          })
          if(that.data.responsibleList.length==0){
            that.setData({
              disabled:true
            })
          }else{
            that.setData({
              disabled:false
            })
          }
        }
      }
      })
    }
    
  },
  getkslist(id,depid){
    let that = this
    that.setData({
      hospitalDepartList:[]
    })
    wx.request({
      url: app.globalData.serverUrlgdb + '/hosPart/list/by/'+id,
        method: 'get',
        header: {
        'token': wx.getStorageSync('token')
      },
      success:res=>{
        let resp = res.data
        if(resp.code == 200){
          if(resp.data.hospitalDepartList.length!=0){
            if(resp.data.hospitalDepartList.length==1){
              resp.data.hospitalDepartList[0].checked = true
            }else{
              resp.data.hospitalDepartList.map(item=>{
                item.checked=false
              })
            }
          }
          if(resp.data.hospitalDepartList.length>1){
            let obj = {
              deptName:'全院',
              deptId:'',
              checked:false
            }
            resp.data.hospitalDepartList.unshift(obj)
            if(depid!=''){
              resp.data.hospitalDepartList.map(item=>{
                if(item.deptId==depid){
                  item.checked = true
                }else{
                  item.checked = false
                }
              })
            }else{
              resp.data.hospitalDepartList[0].checked = true
            }
            
          }
          that.setData({
            hospitalDepartList:resp.data.hospitalDepartList
          })
          console.log(resp.data.hospitalDepartList,'科室');
        }
      }
    })
  },
  bindChangedept(e){
    console.log(e);
    let that = this
    let id = e.currentTarget.dataset.value
    let hospitalDepartList = that.data.hospitalDepartList
    hospitalDepartList.map(item => {
      if (id == item.deptId) {
        item.checked = true
      } else {
        item.checked = false
      }
    })
    that.setData({
      hospitalDepartList,
    })
  },
  bindChange(e) {
    console.log(e,'9999');
    let that = this
    let id = e.currentTarget.dataset.value
    let responsibleList = that.data.responsibleList
    responsibleList.map(item => {
      if (id == item.hospitalId) {
        item.checked = true
        that.setData({
          idactive:item.hospitalId
        })
      } else {
        item.checked = false
      }
    })
    that.setData({
      responsibleList,
      disabled: responsibleList.some(x => x.checked) ? false : true
    })
    that.getkslist(id,'')
  },
  bindtapNext() {
    let that = this
    let lock = null
    if (!that.data.isLock) {
      that.setData({
        isLock: new Date().getTime()
      })
    } else {
      lock = new Date().getTime()
    }
    if (that.data.isLock && lock) {
      if (lock - that.data.isLock < app.globalData.isNumLock) {
        return
      } else {
        that.setData({
          isLock: lock
        })
      }
    }
    let queryFrom = that.data.queryFrom
    that.data.responsibleList.map(item => {
      if (item.checked) {
        queryFrom.hospitalId = item.hospitalId
        queryFrom.hospitalName = item.hospitalName
      }
    })
    that.data.hospitalDepartList.map(item => {
      if (item.checked) {
        queryFrom.hospitalDepartId = item.deptId
        queryFrom.deptName = item.deptName
      }
    })
    that.setData({
      queryFrom
    })
    const pages = getCurrentPages(); //获取当前页面
    const prevPage = pages[pages.length - 2]; //上一页面
    prevPage.setData({ //修改上一页面的某项值
      queryFrom
    })
    wx.navigateBack()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: app.globalData.userInfo,
    })
    this.getTyoeList(JSON.parse(options.id),options.istype)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getTyoeList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})